unit partymemberselectionformunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  StdCtrls, fpjson, fgl, database;

type

  TIntList = specialize TFPGList<Integer>;

  { TPartyMemberSelectionForm }

  TPartyMemberSelectionForm = class(TForm)
    PartyMemberComboBox: TComboBox;
    EcCancelBitBtn: TBitBtn;
    EcOkBitBtn: TBitBtn;
    EcResultLineBevel: TBevel;
    EcResultPanel: TPanel;
    PartyMemberGroupBox: TGroupBox;
  private
    ActorIds: TIntList;
  public
    function SelectPartyMember(Db: TDatabase; Current: Integer;
             OthersList: TJSONArray): Integer;
  end;

var
  PartyMemberSelectionForm: TPartyMemberSelectionForm;

implementation

{$R *.lfm}

{ TPartyMemberSelectionForm }

function TPartyMemberSelectionForm.SelectPartyMember(Db: TDatabase;
  Current: Integer; OthersList: TJSONArray): Integer;

  function IsExcludableItem(ActorId: Integer): Boolean;
  var
    I: Integer;
  begin
    IsExcludableItem := False;

    for I := 0 to OthersList.Count -1 do
      if OthersList.Integers[I] = ActorId then begin
        IsExcludableItem := True;
        Exit
      end
  end;

  procedure FillActorList;
  var
    I: Integer;
    FirstItem: Integer = 0;
  begin
    with PartyMemberComboBox.Items do begin
      Clear;

      for I := 1 to Db.Actors.Count -1 do begin
        if I = Current then
          FirstItem := Count;

        if (I = Current) or not IsExcludableItem(I) then begin
          Add(Db.GetActorName(I));
          ActorIds.Add(I);
        end;
      end;
    end;

    PartyMemberComboBox.ItemIndex := FirstItem;
  end;

begin
  // TODO
  ActorIds := TIntList.Create;

  FillActorList;

  ShowModal;

  SelectPartyMember := -1;
  if (ModalResult = mrOK) and (PartyMemberComboBox.ItemIndex >= 0) then
    SelectPartyMember := ActorIds[PartyMemberComboBox.ItemIndex];

  FreeAndNil(ActorIds);
end;

end.

